import router from '@system.router';
import prompt from '@system.prompt';

export default {
    data: {
        ip:"",
        port:"",
        data:""
    },
    initConnectAction:function(code,ip,port){

        var actionData={};
        actionData.ip=ip;
        actionData.port=port;
        var action={};
        action.bundleName="com.example.tcpConnect";
        action.abilityName="tcpConnectTest";
        action.messageCode = code;
        action.data = actionData;
        action.abilityType = 1;
        action.syncOption = 0;
        return action;
    },
    initAction: function (code) {
        var actionData = {};
        var action = {};
        action.bundleName="com.example.tcpConnect";
        action.abilityName="tcpConnectTest";
        action.messageCode = code;
        action.data = actionData;
        action.abilityType = 1;
        action.syncOption = 0;
        return action;
    },

    initAction2: function (code,msg) {
        var actionData = {};
        actionData.msg = msg;
        var action = {};
        action.bundleName="com.example.tcpConnect";
        action.abilityName="tcpConnectTest";
        action.messageCode = code;
        action.data = actionData;
        action.abilityType = 1;
        action.syncOption = 0;
        return action;
    },
    changeIp(e) {
        this.ip = e.value;
        console.log("ip:"+this.ip)
        this.showToast("ip:"+this.ip)
    },
    changePort(e){
        this.port=e.value;
        console.log("port:"+this.port)
        this.showToast("port:"+this.port)
    },
    onConnect:async function(){
        try{
            var action=this.initConnectAction(1001,this.ip,this.port);
            var that=this;
            var result=await FeatureAbility.callAbility(action);
            if(this.ip==""||this.port==""){
                this.showToast("please enter ip or port");
            }
            /*else {
                this.showToast("error,please try again");
            }*/
            console.info("result:"+ result);
            console.info("undercode:"+result.code);
             if(result=="socket_create"&&this.ip!=""&&this.port!="") {
                router.push({
                    uri: 'pages/autoPage/autoPage'
                });
                console.log("undercode:" + result.code);
             } else {
                 this.showToast(result);
             }
        }catch(pluginError){
            console.info(pluginError)
        }


        // try {
        //     action = this.initAction(1003);
        //     var that = this;
        //     var result = await FeatureAbility.subscribeAbilityEvent(action,function (msgdata) {
        //         console.info(" batteryLevel info is: " + msgdata);
        //         var msgRet = JSON.parse(msgdata).data;
        //         that.printData(msgRet.msg);
        //         that.showToast(" batteryState change: " + msgRet.msg);
        //         this.data=msgRet.msg;
        //     });
        //     console.info(" subscribeCommonEvent result = " + result);
        // } catch (pluginError) {
        //     console.error("subscribeCommonEvent error : result= " + result + JSON.stringify(pluginError));
        // }



    }, printData(msg){
        if(this.outcont != null || this.outcont != ""){
            this.outcont = msg + "\n" + this.outcont;
        } else {
            this.outcont = msg;
        }
    },
    showToast:function(msg){
        prompt.showToast({
            message:msg
        })
    }
}
